package com.zhanglijie.base.threadmethod.callable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/3/13 0013 1:15
 */
public class Test {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Mythread mythread = new Mythread();
        FutureTask<Integer> futureTask = new FutureTask<>(mythread);
        Thread thread = new Thread(futureTask);
        thread.start();
        Integer result = futureTask.get();
        System.out.println(result);

        //futureTask也可以交给线程池的处理
        System.out.println("线程池 完成futureTask =========");
        Mythread mythread2 = new Mythread();
        FutureTask<Integer> futureTask2 = new FutureTask<>(mythread2);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 2, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());
        threadPoolExecutor.execute(futureTask2);
        futureTask2.get();

    }
}
